package service;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import static util.DBUtil.conn;

// 场次服务类
public class ScreeningService {

    public static void addScreening(String id, String movieId, String roomId, String startTime, double price) {

        String title = MovieService.getMovieById(movieId);
        String room = ScreeningRoomService.getRoomById(roomId);
        try(PreparedStatement stmt=conn.prepareStatement("SELECT * FROM screening WHERE id=?")){
            stmt.setString(1,id);
            if(stmt.executeQuery().next()){
                System.out.println("该场次已存在");
                return;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try(PreparedStatement statement = conn.prepareStatement("INSERT INTO screening (id, movie, room, start_time, price) VALUES (?, ?, ?, ?, ?)")) {
            statement.setString(1, id);
            statement.setString(2, title);
            statement.setString(3, room);
            statement.setString(4, startTime);
            statement.setDouble(5, price);
            statement.executeUpdate();
            LogService.logAction("系统", "添加场次: " + title + " - " + startTime);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void updateScreening(String id, String movie, String room, String startTime, double price) {
        if (if_not_exist(id)) return;
        try(PreparedStatement ptmt=conn.prepareStatement("UPDATE screening SET movie=?, room=?, start_time=?, price=? WHERE id=?")){
            ptmt.setString(1,movie);
            ptmt.setString(2,room);
            ptmt.setString(3,startTime);
            ptmt.setDouble(4,price);
            ptmt.setString(5,id);
            ptmt.executeUpdate();
            LogService.logAction("系统", "更新场次: " + id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean deleteScreening(String id) {
        if (if_not_exist(id)) return false;
        try(PreparedStatement ptmt=conn.prepareStatement("DELETE FROM screening WHERE id=?")){
            ptmt.setString(1,id);
            ptmt.executeUpdate();
            LogService.logAction("系统", "删除场次: " + id);
            return true;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private static boolean if_not_exist(String id) {
        try(PreparedStatement ptmt=conn.prepareStatement("SELECT * FROM screening WHERE id=?")){
            ptmt.setString(1,id);
            if(!ptmt.executeQuery().next()){
                System.out.println("该场次不存在");
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }



    public static String getScreeningById(String id) {
        try(PreparedStatement ptmt=conn.prepareStatement("SELECT movie FROM screening WHERE id=?")){
            ptmt.setString(1,id);
            return ptmt.executeQuery().next()?ptmt.executeQuery().getString(1):null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

